package com.cy.action.backuser;

import java.util.ArrayList;
import java.util.List;

import com.cy.common.BaseAction;
import com.cy.condition.BackRoleCondition;
import com.cy.entity.BackPurview;
import com.cy.entity.BackRole;
import com.cy.entity.BackRolePurView;
import com.cy.service.BackPurviewService;
import com.cy.service.BackRoleService;
import com.cy.service.BackUserService;

public class BackRoleManageAction extends BaseAction{

	private static final long serialVersionUID = -3723182304218538115L;

	private BackPurviewService backPurviewService;
	private BackRoleService backRoleService;
	private BackUserService backUserService;
	
	private BackRoleCondition condition;
	private List<BackRole> backroles;
	private List<BackRole> notBackroles;
	
	private BackRole role;

	private String control;
	
	private List<BackPurview> bpLevelIs;
	private List<BackPurview> bpLevelIIs;
	
	private String backUsersStr;
	
	public String execute() {
		backroles=backRoleService.listBackRoleByCondition(0, 0, condition);
		return SUCCESS;
	}
	
	public String addNewRole(){
		bpLevelIs=backPurviewService.listBackPurviewByLevel(0);
		bpLevelIIs=new ArrayList<BackPurview>();
		for(BackPurview backPurview:bpLevelIs){
			bpLevelIIs.addAll(backPurviewService.listBackPurviewByParent(backPurview.getId()));
		}
		return SUCCESS;
	}

	public String editNewRole(){
		role=backRoleService.findDetailBackRoleById(condition.getId());
		List<Long> selectPID=new ArrayList<Long>();
		for(BackRolePurView backRolePurView:role.getRolePurviewSet()){
			selectPID.add(backRolePurView.getPurview().getId());  //角色对应的权限集合
		}
		
		bpLevelIs=backPurviewService.listBackPurviewByLevel(0);
		for(BackPurview purview:bpLevelIs){
			if(selectPID.contains(purview.getId())){
				purview.setIsSelect(1);
			}
		}
		bpLevelIIs=new ArrayList<BackPurview>();
		for(BackPurview backPurview:bpLevelIs){
			bpLevelIIs.addAll(backPurviewService.listBackPurviewByParent(backPurview.getId()));
		}
		
		for(BackPurview purview:bpLevelIIs){
			if(selectPID.contains(purview.getId())){
				purview.setIsSelect(1);
			}
		}
		return SUCCESS;
	}
	
	public String oneRoleRalationBackUsers(){
		role=(BackRole) backRoleService.getObject(condition.getId());
		backroles=backRoleService.findBackUserByRoleId(condition.getId());
		notBackroles=backRoleService.findBackUserNotEqualRoleId(condition.getId());
		return SUCCESS;
	}

	
	public String delRole(){
		BackRole backRole= (BackRole) backRoleService.getObject(condition.getId());
		backRoleService.removeObject(backRole);
		return SUCCESS;
	}
	
	
	public String oneRoleRalationBackUsersUpdate(){
		BackRole backRole=(BackRole) backRoleService.getObject(role.getId());
		backUserService.updateRoleRelationBackUsers(backRole, backUsersStr);
		return SUCCESS;
	}
	
	
	
	public BackPurviewService getBackPurviewService() {
		return backPurviewService;
	}

	public void setBackPurviewService(BackPurviewService backPurviewService) {
		this.backPurviewService = backPurviewService;
	}

	public BackRoleService getBackRoleService() {
		return backRoleService;
	}

	public void setBackRoleService(BackRoleService backRoleService) {
		this.backRoleService = backRoleService;
	}

	public BackRoleCondition getCondition() {
		return condition;
	}

	public void setCondition(BackRoleCondition condition) {
		this.condition = condition;
	}

	public List<BackRole> getBackroles() {
		return backroles;
	}

	public void setBackroles(List<BackRole> backroles) {
		this.backroles = backroles;
	}


	public List<BackPurview> getBpLevelIs() {
		return bpLevelIs;
	}

	public void setBpLevelIs(List<BackPurview> bpLevelIs) {
		this.bpLevelIs = bpLevelIs;
	}

	public List<BackPurview> getBpLevelIIs() {
		return bpLevelIIs;
	}

	public void setBpLevelIIs(List<BackPurview> bpLevelIIs) {
		this.bpLevelIIs = bpLevelIIs;
	}

	public BackRole getRole() {
		return role;
	}

	public void setRole(BackRole role) {
		this.role = role;
	}

	public String getControl() {
		return control;
	}

	public void setControl(String control) {
		this.control = control;
	}

	public List<BackRole> getNotBackroles() {
		return notBackroles;
	}

	public void setNotBackroles(List<BackRole> notBackroles) {
		this.notBackroles = notBackroles;
	}

	public String getBackUsersStr() {
		return backUsersStr;
	}

	public void setBackUsersStr(String backUsersStr) {
		this.backUsersStr = backUsersStr;
	}

	public BackUserService getBackUserService() {
		return backUserService;
	}

	public void setBackUserService(BackUserService backUserService) {
		this.backUserService = backUserService;
	}

	
	

}
